#include <bits/stdc++.h>
using namespace std;

struct Node {
  int a, b;
  bool operator<(const Node &o) const { return min(o.b, a) < min(b, o.a); }
} p[200005];

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> p[i].a >> p[i].b;

  std::sort(p, p + n);

  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    s1 += p[i].a;
    s2 = max(s1, s2) + p[i].b;
  }

  cout << s2 << '\n';

  return 0;
}